<?php

require('config.php');

$cityByUrl = modelProvince::getProvinceByUrl('cities.php');
$balise_title = $cityByUrl->title;
$description = $cityByUrl->meta_title;

$id_menu_left_tree = modelProvince::getProvinceMenuleft($cityByUrl->language_link)->id_menu_left_tree;
$page = new rootLayoutPage();
//-- Menu Page ----------------------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menuPage->setTable('city');
$menuPage->setIdShow($id_menu_left_tree);
$menu->setIdShow($id_menu_left_tree);
$menu->pushMenu($menuPage);

//--------Menu indicator--------------//
$menuIndictor = new layoutMenuIndicator();
$menuIndictor->idTree($id_menu_left_tree);
$menuIndictor->title($balise_title);
$page->setMenuIndicator($menuIndictor);

//************************//

$header = new layoutHeader();
$header->setArrayUrl($arr);
$header->setIdShow($id_menu_left_tree);
$page->setHeader($header);

$page->setMenuPage($menu);

$page->setBaliseTitle($balise_title);
$page->setDescription($description);
$extension = filename_extension($_SERVER['REQUEST_URI']);
if($extension == 'html') $page->setCanonical($html_name);

$Body = new flyLayout(REP_TPL . 'cities.tpl');
$Body->start();
	$listProvinceAll = modelProvince::getList(1);
	$seper=0;
		if($listProvinceAll->size()>5) {
			for($i=1;$i<$listProvinceAll->size()/2;$i++){
				$all = $listProvinceAll->size();
				if($all-($i+$i)<=$i){ $seper = $i; break;}

			}
		}
	if($listProvinceAll->size() > 0){
		if($seper==0){
			$listProvince1  = modelProvince::getList(1);
			if($listProvince1->size() > 0){
				foreach($listProvince1 as $k1 => $province1){					
					$urlProvince1 = LANGUAGE_FOLDER.'/'.$province1->url_rewriting.'-'.$province1->language_link.'.html';
					$language_link_1 = $province1->language_link;
					$listAllProvince_1 = modelProvince::getListProvinceByid($language_link_1);
					if($listAllProvince_1->size() > 0){
						$Body->setVariable('title_province_1',$province1->title);
						$Body->setVariable('url_province_1',$urlProvince1);
						$Body->showBlock('url_province_1_no_child');
						$Body->hideBlock('url_province_1_have_child');
					}else{
						$Body->setVariable('title_province_1',$province1->title);
						$Body->showBlock('url_province_1_have_child');
						$Body->hideBlock('url_province_1_no_child');
					}

					$Body->resetList('city_1');
						$listPDistrict1 = modelProvince::getCityByIDProvince($province1->language_link,1);
						if($listPDistrict1->size() > 0){
							foreach($listPDistrict1 as $kd1 => $city_1){
								$url_city_1 = LANGUAGE_FOLDER.'/'.$city_1->url_rewriting.'-'.$city_1->language_link.'.html';
								$language_link_city_1 = $city_1->language_link;
								$listAllCity_1 = modelCity::getListCityByid($language_link_city_1);
								if($listAllCity_1->size() > 0){
									$Body->setVariable('title_city_1',trim($city_1->title));
									$Body->setVariable('url_city_1',$url_city_1);
									$Body->showBlock('url_city_1_no_child');
									$Body->hideBlock('url_city_1_have_child');
								}else{
									$Body->setVariable('title_city_1',trim($city_1->title));
									$Body->showBlock('url_city_1_have_child');
									$Body->hideBlock('url_city_1_no_child');
								}							
									$Body->resetList('district_1');
									$listDistrict1 = modelProvince::getDistrctByIDCity($city_1->language_link,1);
									if($listDistrict1->size() > 0){
										foreach($listDistrict1 as $kd1 => $district_1){
											$url_district_1 = LANGUAGE_FOLDER.'/'.$district_1->url_rewriting.'-'.$district_1->language_link.'.html';
											$language_link_district_1 = $district_1->language_link;
											$listAllDistrict_1 = modelDistrict::getListDistrictByid($language_link_district_1);
											if($listAllDistrict_1->size() > 0){
												$Body->setVariable('title_district_1',trim($district_1->title));
												$Body->setVariable('url_district_1',$url_district_1);
												$Body->showBlock('url_district_1_no_child');
												$Body->hideBlock('url_district_1_have_child');
											}else{
												$Body->setVariable('title_district_1',trim($district_1->title));
												$Body->showBlock('url_district_1_have_child');
												$Body->hideBlock('url_district_1_no_child');
											}
											$Body->parseList('district_1');
										}
										$Body->setVariable('oul1',"<ul>");
										$Body->setVariable('cul1',"</ul>");
									}

								$Body->parseList('city_1');
							}
							$Body->showBlock('provinces_d1');
						}else $Body->hideBlock('provinces_d1');

					$Body->parseList('provinces_1');
				}
			}
		}else{
			$limit1 = "0,".$seper;
			$listProvince1  = modelProvince::getList(1,$limit1);
			if($listProvince1->size() > 0){
				foreach($listProvince1 as $k1 => $province1){
					$urlProvince1 = LANGUAGE_FOLDER.'/'.$province1->url_rewriting.'-'.$province1->language_link.'.html';
					$language_link_1 = $province1->language_link;
					$listAllProvince_1 = modelProvince::getListProvinceByid($language_link_1);
					if($listAllProvince_1->size() > 0){
						$Body->setVariable('title_province_1',$province1->title);
						$Body->setVariable('url_province_1',$urlProvince1);
						$Body->showBlock('url_province_1_no_child');
						$Body->hideBlock('url_province_1_have_child');
					}else{
						$Body->setVariable('title_province_1',$province1->title);
						$Body->showBlock('url_province_1_have_child');
						$Body->hideBlock('url_province_1_no_child');
					}
					$Body->resetList('city_1');
						$listPDistrict1 = modelProvince::getCityByIDProvince($province1->language_link,1);
						if($listPDistrict1->size() > 0){
							foreach($listPDistrict1 as $kd1 => $city_1){
								$url_city_1 = LANGUAGE_FOLDER.'/'.$city_1->url_rewriting.'-'.$city_1->language_link.'.html';
								$language_link_city_1 = $city_1->language_link;
								$listAllCity_1 = modelCity::getListCityByid($language_link_city_1);
								if($listAllCity_1->size() > 0){
									$Body->setVariable('title_city_1',trim($city_1->title));
									$Body->setVariable('url_city_1',$url_city_1);
									$Body->showBlock('url_city_1_no_child');
									$Body->hideBlock('url_city_1_have_child');
								}else{
									$Body->setVariable('title_city_1',trim($city_1->title));
									$Body->showBlock('url_city_1_have_child');
									$Body->hideBlock('url_city_1_no_child');
								}
									$Body->resetList('district_1');
									$listDistrict1 = modelProvince::getDistrctByIDCity($city_1->language_link,1);
									if($listDistrict1->size() > 0){
										foreach($listDistrict1 as $d1 => $district_1){											
											$url_district_1 = LANGUAGE_FOLDER.'/'.$district_1->url_rewriting.'-'.$district_1->language_link.'.html';
											$language_link_district_1 = $district_1->language_link;
											$listAllDistrict_1 = modelDistrict::getListDistrictByid($language_link_district_1);
											if($listAllDistrict_1->size() > 0){
												$Body->setVariable('title_district_1',trim($district_1->title));
												$Body->setVariable('url_district_1',$url_district_1);
												$Body->showBlock('url_district_1_no_child');
												$Body->hideBlock('url_district_1_have_child');
											}else{
												$Body->setVariable('title_district_1',trim($district_1->title));
												$Body->showBlock('url_district_1_have_child');
												$Body->hideBlock('url_district_1_no_child');
											}
											$Body->parseList('district_1');
										}
										$Body->setVariable('oul1',"<ul>");
										$Body->setVariable('cul1',"</ul>");
									}
								$Body->parseList('city_1');
							}
							$Body->showBlock('provinces_d1');
						}else $Body->hideBlock('provinces_d1');

					$Body->parseList('provinces_1');
				}
			}
			//$limit2 = (($listProvinceAll->size()/2)+1).",".$listProvinceAll->size();
			$limit2 = ($seper+1).",".$seper;
			$listProvince2  = modelProvince::getList(1,$limit2);
			if($listProvince2->size() > 0){
				foreach($listProvince2 as $k2 => $province2){
					$urlProvince2 = LANGUAGE_FOLDER.'/'.$province2->url_rewriting.'-'.$province2->language_link.'.html';
					$language_link_2 = $province2->language_link;
					$listAllProvince_2 = modelProvince::getListProvinceByid($language_link_2);
					if($listAllProvince_2->size() > 0){
						$Body->setVariable('title_province_2',$province2->title);
						$Body->setVariable('url_province_2',$urlProvince2);
						$Body->showBlock('url_province_2_no_child');
						$Body->hideBlock('url_province_2_have_child');
					}else{
						$Body->setVariable('title_province_2',$province2->title);
						$Body->showBlock('url_province_2_have_child');
						$Body->hideBlock('url_province_2_no_child');
					}
					$Body->resetList('city_2');
						$listPDistrict2 = modelProvince::getCityByIDProvince($province2->language_link,1);
						if($listPDistrict2->size() > 0){
							foreach($listPDistrict2 as $kd2 => $city_2){
								$url_city_2 = LANGUAGE_FOLDER.'/'.$city_2->url_rewriting.'-'.$city_2->language_link.'.html';
								$language_link_city_2 = $city_2->language_link;
								$listAllCity_2 = modelCity::getListCityByid($language_link_city_2);
								if($listAllCity_2->size() > 0){
									$Body->setVariable('title_city_2',trim($city_2->title));
									$Body->setVariable('url_city_2',$url_city_2);
									$Body->showBlock('url_city_2_no_child');
									$Body->hideBlock('url_city_2_have_child');
								}else{
									$Body->setVariable('title_city_2',trim($city_2->title));
									$Body->showBlock('url_city_2_have_child');
									$Body->hideBlock('url_city_2_no_child');
								}
								$Body->resetList('district_2');
									$listDistrict2 = modelProvince::getDistrctByIDCity($city_2->language_link,1);
									if($listDistrict2->size() > 0){
										foreach($listDistrict2 as $d2 => $district_2){
											$url_district_2 = LANGUAGE_FOLDER.'/'.$district_2->url_rewriting.'-'.$district_2->language_link.'.html';
											$language_link_district_2 = $district_2->language_link;
											$listAllDistrict_2 = modelDistrict::getListDistrictByid($language_link_district_2);
											if($listAllDistrict_2->size() > 0){
												$Body->setVariable('title_district_2',trim($district_2->title));
												$Body->setVariable('url_district_2',$url_district_2);
												$Body->showBlock('url_district_2_no_child');
												$Body->hideBlock('url_district_2_have_child');
											}else{
												$Body->setVariable('title_district_2',trim($district_2->title));
												$Body->showBlock('url_district_2_have_child');
												$Body->hideBlock('url_district_2_no_child');
											}
											$Body->parseList('district_2');
										}
										$Body->setVariable('oul2',"<ul>");
										$Body->setVariable('cul2',"</ul>");
									}
								$Body->parseList('city_2');
							}
							$Body->showBlock('provinces_d2');
						}else $Body->hideBlock('provinces_d2');
					$Body->parseList('provinces_2');
				}
			}
			$limit3 = ($seper+$seper+1).",".$seper;
			$listProvince3  = modelProvince::getList(1,$limit3);
			if($listProvince3->size() > 0){
				foreach($listProvince3 as $k3 => $province3){
					$urlProvince3 = LANGUAGE_FOLDER.'/'.$province3->url_rewriting.'-'.$province3->language_link.'.html';
					$language_link_3 = $province3->language_link;
					$listAllProvince_3 = modelProvince::getListProvinceByid($language_link_3);
					if($listAllProvince_3->size() > 0){
						$Body->setVariable('title_province_3',$province3->title);
						$Body->setVariable('url_province_3',$urlProvince3);
						$Body->showBlock('url_province_3_no_child');
						$Body->hideBlock('url_province_3_have_child');
					}else{
						$Body->setVariable('title_province_3',$province3->title);
						$Body->showBlock('url_province_3_have_child');
						$Body->hideBlock('url_province_3_no_child');
					}
					$Body->resetList('city_3');
						$listPDistrict3 = modelProvince::getCityByIDProvince($province3->language_link,1);
						if($listPDistrict3->size() > 0){
							foreach($listPDistrict3 as $kd3 => $city_3){
								$url_city_3 = LANGUAGE_FOLDER.'/'.$city_3->url_rewriting.'-'.$city_3->language_link.'.html';
								$language_link_city_3 = $city_3->language_link;
								$listAllCity_3 = modelCity::getListCityByid($language_link_city_3);
								if($listAllCity_3->size() > 0){
									$Body->setVariable('title_city_3',trim($city_3->title));
									$Body->setVariable('url_city_3',$url_city_3);
									$Body->showBlock('url_city_3_no_child');
									$Body->hideBlock('url_city_3_have_child');
								}else{
									$Body->setVariable('title_city_3',trim($city_3->title));
									$Body->showBlock('url_city_3_have_child');
									$Body->hideBlock('url_city_3_no_child');
								}
									$Body->resetList('district_3');
									$listDistrict3 = modelProvince::getDistrctByIDCity($city_3->language_link,1);
									if($listDistrict3->size() > 0){
										foreach($listDistrict3 as $d3 => $district_3){
											$url_district_3 = LANGUAGE_FOLDER.'/'.$district_3->url_rewriting.'-'.$district_3->language_link.'.html';
											$language_link_district_3 = $district_3->language_link;
											$listAllDistrict_3 = modelDistrict::getListDistrictByid($language_link_district_3);
											if($listAllDistrict_3->size() > 0){
												$Body->setVariable('title_district_3',trim($district_3->title));
												$Body->setVariable('url_district_3',$url_district_3);
												$Body->showBlock('url_district_3_no_child');
												$Body->hideBlock('url_district_3_have_child');
											}else{
												$Body->setVariable('title_district_3',trim($district_3->title));
												$Body->showBlock('url_district_3_have_child');
												$Body->hideBlock('url_district_3_no_child');
											}
											$Body->parseList('district_3');
										}
										$Body->setVariable('oul3',"<ul>");
										$Body->setVariable('cul3',"</ul>");
									}
								$Body->parseList('city_3');
							}
							$Body->showBlock('provinces_d3');
						}else $Body->hideBlock('provinces_d3');
					$Body->parseList('provinces_3');
				}
			}
		}
	}
	//$Body->setVariable('page',$newContent);
$page->setIndex(1);
$page->setFollow(1);
$Body->stop();
$page->setBody($Body);
$page->display();

?>